package xj.toolkit.redis.shard;

import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.ZParams;
import redis.clients.util.Hashing;
import xj.toolkit.redis.Jedis2Commands;
import xj.toolkit.redis.PipelineExecutor;
import xj.toolkit.redis.TransactionExector;

public class ShardedJedis2 extends ShardedJedis implements Jedis2Commands {

	protected ShardedJedis2Pool dataSource = null;

	public ShardedJedis2(List<JedisShardInfo> shards) {
		super(shards);
	}

	public ShardedJedis2(List<JedisShardInfo> shards, Hashing algo) {
		super(shards, algo);
	}

	public ShardedJedis2(List<JedisShardInfo> shards, Pattern keyTagPattern) {
		super(shards, keyTagPattern);
	}

	public ShardedJedis2(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {
		super(shards, algo, keyTagPattern);
	}

	@Override
	public void sync(PipelineExecutor executor) {

	}

	@Override
	public List<Object> syncAndReturnAll(PipelineExecutor executor) {
		return null;
	}

	@Override
	public List<Object> exec(TransactionExector exector) {
		return null;
	}

	@Override
	public Long del(String[] as) {
		Preconditions.checkArgument(as != null && as.length > 0);
		long integerReply = 0;

		for (String s : as) {
			Jedis j = getShard(s);
			integerReply += j.del(s);
		}
		return integerReply;
	}

	public void setDataSource(ShardedJedis2Pool shardedJedisPool) {
		this.dataSource = shardedJedisPool;
	}

	@Override
	public void close() {
		if (dataSource != null) {
			boolean broken = false;

			for (Jedis jedis : getAllShards()) {
				if (jedis.getClient().isBroken()) {
					broken = true;
					break;
				}
			}
			ShardedJedis2Pool pool = this.dataSource;
			this.dataSource = null;
			if (broken) {
				pool.returnBrokenResource(this);
			} else {
				pool.returnResource(this);
			}

		} else {
			disconnect();
		}
	}

	@Override
	public Set<String> keys(String s) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String randomKey() {
		throw new UnsupportedOperationException();
	}

	@Override
	public String rename(String s, String s1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long renamenx(String s, String s1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<String> mget(String[] as) {
		Preconditions.checkArgument(as != null && as.length > 0);
		List<String> reply = Lists.newArrayList();

		for (String s : as) {
			Jedis j = getShard(s);
			reply.addAll(j.mget(s));
		}
		return reply;
	}

	@Override
	public String mset(String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long msetnx(String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String rpoplpush(String s, String s1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long smove(String s, String s1, String s2) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<String> sinter(String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long sinterstore(String s, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<String> sunion(String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long sunionstore(String s, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Set<String> sdiff(String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long sdiffstore(String s, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<String> blpop(int i, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long sort(String s, SortingParams sortingparams, String s1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long sort(String s, String s1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List brpop(int i, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long zunionstore(String s, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long zunionstore(String s, ZParams zparams, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long zinterstore(String s, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long zinterstore(String s, ZParams zparams, String[] as) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Long lpushx(String s, String s1) {
		throw new UnsupportedOperationException();
	}

}
